#!/usr/bin/env python
# -*- coding: utf-8 -*-
# (c) Anton A Nesterov
# License: Apache2.0

from google.appengine.ext import db
from google.appengine.ext import blobstore



class Collection(db.Model):

  title = db.StringProperty()
  editable = db.BooleanProperty(default=True)
  time  = db.DateTimeProperty(auto_now=True)

  @classmethod
  def create(cls, title, lock=False):
    if not cls.get(title):
      rec = cls( title = title, editable=not lock )
      db.put(rec)
    return
  @classmethod
  def get(cls, title):
    rec = cls.all().order('time').filter('title =',unicode(title))
    return rec.get()

  @classmethod
  def get_or_put(cls, title, lock=False):
    cls.create(title, lock)
    return cls.get(title)

  @classmethod
  def put(cls, **kv):

    title = kv.get('title')
    same = cls.get(title)

    assert title and not same
    assert len(kv) < 5 #for any case

    id = kv.get('id')
    rec = cls.get_by_id(long( id ))

    if rec:
      rec.title = title
    else:
      rec = cls(title=title)

    db.put(rec)

    return




  @classmethod
  def lst(cls, **kv):

    rec = cls.all().order('-time')


    rec = rec.run(
	    limit=int ( kv.get('limit') or 50 ),
	    offset=int ( kv.get('offset') or 0 )
	  )

    return [{
      "title":i.title,
      "date":i.time.strftime("%b %d %Y %H:%M:%S"),
      "id":i.key().id_or_name()
      } for i in rec]

  @classmethod
  def delete(cls, id):
    rec = cls.get_by_id( long(id) )
    if rec:

      db.delete(rec)
    return

  @classmethod
  def rem(cls, title):
    rec = cls.get(unicode(title))
    if rec:
      db.delete(rec)
    return

  @classmethod
  def rem_safe(cls, title):
    rec = cls.get(unicode(title))

    if rec and rec.editable:
      db.delete(rec)
      return True
    return False

  @classmethod
  def upd(cls, title, new_title):
    rec = cls.get(unicode(title))
    if rec and rec.editable:
      rec.title = unicode(new_title)
      db.put(rec)
    return


class File(db.Model):

  title  = db.StringProperty()
  colle  = db.ReferenceProperty(Collection)
  cover  = db.BooleanProperty(default=False)

  thumb_100  = blobstore.BlobReferenceProperty()
  thumb_250  = blobstore.BlobReferenceProperty()
  thumb_400  = blobstore.BlobReferenceProperty()
  blob_key   = blobstore.BlobReferenceProperty()

  time  = db.DateTimeProperty(auto_now=True)

  @classmethod
  def put(cls, **kv):

    rec = cls( **kv )
    db.put(rec)
    return

  @classmethod
  def get(cls, title):
    rec = cls.all().order('time').filter('title =',title)
    return rec.get()

  @classmethod
  def get_cover(cls, **kv):
    rec = File.all()
    col = kv.get('collection')
    if col:
      col = col.decode('utf-8')
      rec.filter('colle =', Collection.get(col))
      rec.filter('cover =', True)
    else:
      return
    return rec.get()

  @classmethod
  def lst(cls, **kv):

    rec = cls.all().order('-time')

    col = kv.get('collection')
    if col:
      col = col.decode('utf-8')
      rec.filter('colle =', Collection.get(col))

    return rec.run(
	  limit=int ( kv.get('limit') or 50 ),
	  offset=int ( kv.get('offset') or 0 )
      )

  @classmethod
  def setcover(cls, key):
    rec = cls.get_by_id(key)

    col = cls.all().filter('colle =', rec.colle)

    for i in col:
      i.cover = False
      db.put(i)

    rec.cover = True
    db.put(rec)

    return

  @classmethod
  def rem(cls, key):

    rec = cls.get_by_id(long(key))
    if rec:
      blobstore.delete([
	      rec.blob_key.key(),
	      rec.thumb_100.key(),
	      rec.thumb_250.key(),
	      rec.thumb_400.key()
	    ])
      db.delete(rec)
    return

  @classmethod
  def rem_collection(cls, key):

    rec = Collection.get_by_id(long(key))
    if rec:

      files = cls.all().filter('colle =', rec)
      for f in files:

        if f.blob_key and f.thumb_400:
          blobstore.delete([
	          f.blob_key.key(),
	          f.thumb_100.key(),
	          f.thumb_250.key(),
	          f.thumb_400.key()
	        ])

        db.delete(f)

      db.delete(rec)

    return


  @classmethod
  def upd(cls, title, new_title):
    rec = cls.get(title)
    rec.title = new_title
    db.put(rec)
    return

